<?php

class Tweet {
	var $id, $text, $author, $author_id, $date, $replyTo, $icon, $comment;
}

function getTweetList($user_id, $listname, $numberOfTweets = 200) {
	global $twitter;

	$tweet_limit = $numberOfTweets;
	$max_per_page = 200;
	$tweets = array();

	$read = 0;
	$page = 1;

	if ($tweet_limit > $max_per_page)
		$per_page = $tweet_limit;
	else
		$per_page = $max_per_page;
		
	$params = array();
	$params["slug"] = $listname;
	$params["owner_id"] = $user_id;
	$params["per_page"] = $per_page;

	while ($read <= $tweet_limit) {
		$params["page"] = $page;
		
		try {
			$tweet_list = $twitter->get_listsStatuses($params)->response;
		}
		catch (Exception $e) {
			return false;
		}
		
		foreach ($tweet_list as $tweet) {
			$new_tweet = new Tweet();
			$new_tweet->id = $tweet["id_str"]; 
			$new_tweet->text = $tweet["text"];
			$new_tweet->author = $tweet["user"]["screen_name"];
			$new_tweet->author_id = $tweet["user"]["id"];
			$new_tweet->date = strtotime($tweet["created_at"]);
			$new_tweet->replyTo = $tweet["in_reply_to_status_id_str"];
			$new_tweet->icon = $tweet["user"]["profile_image_url"];
			
			array_push($tweets, $new_tweet);
		}
		
		$read += $per_page;
		$page++;
	}
	
	return $tweets;
}

function getTweetListForDate($user_id, $listname, $date) {
	global $twitter;

	$max_per_page = 200;
	$tweets = array();

	$date = date("Y-m-d", $date);
	
	$readNextPage = true;
	$page = 1;
			
	$params = array();
	$params["slug"] = $listname;
	$params["owner_id"] = $user_id;
	$params["per_page"] = $max_per_page;
	
	while ($readNextPage) {
		$params["page"] = $page;
		
		try {
			$tweet_list = $twitter->get_listsStatuses($params)->response;
		}
		catch (Exception $e) {
			return false;
		}
		
		$i = 0;
		$length = count($tweet_list) - 1;
		
		// ツイートを一つずつ処理する
		foreach ($tweet_list as $tweet) {
			$tweet_date = date("Y-m-d", strtotime($tweet['created_at']));
			
			// ツイートの情報を変数に入れる
			if ($tweet_date == $date) {
				$new_tweet = new Tweet();
				$new_tweet->id = $tweet["id_str"]; 
				$new_tweet->text =$tweet["text"];
				$new_tweet->author = $tweet["user"]["screen_name"];
				$new_tweet->author_id = $tweet["user"]["id"];
				$new_tweet->date = strtotime($tweet["created_at"]);
				$new_tweet->replyTo = $tweet["in_reply_to_status_id_str"];
				$new_tweet->icon = $tweet["user"]["profile_image_url"];
				
				array_push($tweets, $new_tweet);
			}
			
			// 次のページを読み込む必要があるのか
			if ($i == $length) {
				if ($tweet_date > $date) {
					$readNextPage = true;
				}
				else {
					$readNextPage = false;
				}
			}
			
			$i++;
		}
		
		$page++;
	}
	
	return $tweets;
}

function getTweetListForSpan($user_id, $listname, $dateStart, $dateEnd) {
	global $twitter;

	$max_per_page = 200;
	$tweets = array();
	
	$dateStart = date("Y-m-d", $dateStart);
	$dateEnd = date("Y-m-d", $dateEnd);
	
	$readNextPage = true;
	$page = 1;
			
	$params = array();
	$params["slug"] = $listname;
	$params["owner_id"] = $user_id;
	$params["per_page"] = $max_per_page;
	
	$span_start = false;
	$finished = false;
	
	while ($readNextPage) {
		$params["page"] = $page;
		$params["per_page"] = $max_per_page;
		
		try {
			$tweet_list = $twitter->get_listsStatuses($params)->response;
		}
		catch (Exception $e) {
			return false;
		}
		
		$i = 0;
		
		// ツイートを一つずつ処理する
		foreach ($tweet_list as $tweet) {
			$tweet_date = date("Y-m-d", strtotime($tweet['created_at']));
			
			// ツイートの情報を変数に入れる
			if ($tweet_date >= $dateStart && $tweet_date <= $dateEnd) {
				$span_start = true;
				
				$new_tweet = new Tweet();
				$new_tweet->id = $tweet["id_str"]; 
				$new_tweet->text =$tweet["text"];
				$new_tweet->author = $tweet["user"]["screen_name"];
				$new_tweet->author_id = $tweet["user"]["id"];
				$new_tweet->date = strtotime($tweet["created_at"]);
				$new_tweet->replyTo = $tweet["in_reply_to_status_id_str"];
				$new_tweet->icon = $tweet["user"]["profile_image_url"];
				
				array_push($tweets, $new_tweet);
			}
			else {
				if ($span_start) {
					$finished = true;
					break;
				}
			}
			
			$i++;
		}
		
		if (count($tweet_list) < $max_per_page) {
			$finished = true;
		}
		
		// 次のページを読み込む必要があるのか
		if ($finished) {
			$readNextPage = false;
		} else {
			$readNextPage = true;
		}
		
		$page++;
	}
	
	return $tweets;
}

function getTweetsUntilTweetId($user_id, $listname, $continue_until_tweet_id) {
	global $twitter;

	$max_per_page = 200;
	$tweets = array();
	
	$readNextPage = true;
	$page = 1;
			
	$params = array();
	$params["slug"] = $listname;
	$params["owner_id"] = $user_id;
	$params["per_page"] = $max_per_page;
	
	$tweet_found = false;
	
	while ($readNextPage) {
		$params["page"] = $page;
		
		try {
			$tweet_list = $twitter->get_listsStatuses($params)->response;
		}
		catch (Exception $e) {
			return false;
		}
		
		$i = 0;
		$length = count($tweet_list) - 1;
		
		// ツイートを一つずつ処理する
		foreach ($tweet_list as $tweet) {
			
			// ツイートの情報を変数に入れる
			$new_tweet = new Tweet();
			$new_tweet->id = $tweet["id_str"]; 
			$new_tweet->text =$tweet["text"];
			$new_tweet->author = $tweet["user"]["screen_name"];
			$new_tweet->author_id = $tweet["user"]["id"];
			$new_tweet->date = strtotime($tweet["created_at"]);
			$new_tweet->replyTo = $tweet["in_reply_to_status_id_str"];
			$new_tweet->icon = $tweet["user"]["profile_image_url"];
			
			array_push($tweets, $new_tweet);
			
			$tweet_id = $tweet["id_str"];
			
			if ($tweet_id == $continue_until_tweet_id) {
				$tweet_found = true;
				break;
			}
			
			$i++;
		}
		
		if ($tweet_found)
			$readNextPage = false;
		else
			$readNextPage = true;
			
		$page++;
	}
	
	return $tweets;
}

?>
